package com.alibaba.citrus.ots.platform.enhance.repository.utils;

import com.alibaba.fastjson.JSON;
import com.epoch.app.otsplatformenhance.domain.dictcode.model.DictCode;
import com.epoch.app.otsplatformenhance.domain.dictvalue.model.DictValue;
import com.epoch.app.otsplatformenhance.model.dto.DictValueSDO;
import com.epoch.app.otsplatformenhance.sdo.DictionarySDO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 转换
 * @author: qianlei
 * @create: 2021/10/17 11:53
 */
public class DictionaryConvert {


//    public static List<DictionarySDO> convertDictCodeSdoList(List<DictCode> dictCodeList){
//        if (CollectionUtils.isEmpty(dictCodeList)){
//            return null;
//        }
//        return dictCodeList.stream().map(convertDictCodeSdoList()).collect(Collectors.toList());
//    }


    public static DictionarySDO convertToSdo(DictCode dictCode, List<DictValue> dictValueList){
        if (dictCode == null || dictCode.getCode() == null){
            return null;
        }
        DictionarySDO dictionarySDO = new DictionarySDO();
        dictionarySDO.setDictId(dictCode.getId());
        dictionarySDO.setGroupCode(dictCode.getGroupCode());
        dictionarySDO.setCode(dictCode.getCode());
        dictionarySDO.setName(dictCode.getName());
        dictionarySDO.setDesc(dictCode.getDesc());
        dictionarySDO.setStatus(dictCode.getStatus());
        dictionarySDO.setFeatures(JSON.parseObject(dictCode.getFeatures(),Map.class));
        dictionarySDO.setCreatorId(dictCode.getCreatorId());
        dictionarySDO.setCreatorName(dictCode.getCreatorName());
        dictionarySDO.setModifierId(dictCode.getModifierId());
        dictionarySDO.setModifierName(dictCode.getModifierName());
        dictionarySDO.setGmtCreate(dictCode.getGmtCreate());
        dictionarySDO.setGmtModified(dictCode.getGmtModified());
        dictionarySDO.setSyncVersion(dictCode.getSyncVersion());
        dictionarySDO.setDictValueList(convertValueSdoList(dictValueList));
        return dictionarySDO;
    }
    public static List<DictValueSDO> convertValueSdoList(List<DictValue> dictValueList){
        if (CollectionUtils.isEmpty(dictValueList)){
            return null;
        }
        return dictValueList.stream().map(DictionaryConvert::convertToValueSdo).collect(Collectors.toList());
    }
    public static DictValueSDO convertToValueSdo(DictValue dictValue){
        DictValueSDO dictValueSdo = new DictValueSDO();
        dictValueSdo.setId(dictValue.getId());
        dictValueSdo.setValueData(dictValue.getValueData());
        dictValueSdo.setValueName(dictValue.getValueName());
        dictValueSdo.setDictCode(dictValue.getDictCode());
        dictValueSdo.setDesc(dictValue.getDesc());
        dictValueSdo.setStatus(dictValue.getStatus());
        dictValueSdo.setPriority(dictValue.getPriority());
//        todo map 非空判断 黔雷
        dictValueSdo.setFeatures(JSON.parseObject(dictValue.getFeatures(), Map.class));
        dictValueSdo.setCreatorId(dictValue.getCreatorId());
        dictValueSdo.setCreatorName(dictValue.getCreatorName());
        dictValueSdo.setModifierId(dictValue.getModifierId());
        dictValueSdo.setModifierName(dictValue.getModifierName());
        dictValueSdo.setGmtCreate(dictValue.getGmtCreate());
        dictValueSdo.setGmtModified(dictValue.getGmtModified());
        dictValueSdo.setSyncVersion(dictValue.getSyncVersion());
        return dictValueSdo;

    }
}
